<template>
  <div class="goods-comment">
    <div class="head">
      <div class="data">
        <p>
          <span>{{ commentData.salesCount }} </span><span>人购买</span>
        </p>
        <p>
          <span> {{ commentData.praisePercent }} </span><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            href="javascript:"
            :class="{ active: item.title === titleLight }"
            @click="handleButtonLight(item)"
            v-for="item in commentData.tags"
            :key="item"
          >
            {{ item.title }}（{{ item.tagCount }}）
          </a>
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a href="javascript:" class="active">默认</a>
      <a href="javascript:">最新</a>
      <a href="javascript:">最热</a>
    </div>
    <div class="list">
      <div class="item" v-for="item in commentListData.items" :key="item">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ formatNickname(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i
              class="iconfont icon-wjx01"
              v-for="y in item.score"
              :key="'s' + y"
            ></i>
            <i
              class="iconfont icon-wjx02"
              v-for="x in 5 - item.score"
              :key="'k' + x"
            ></i>
            <span class="attr">{{ formatAttrs(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">
            {{ item.content }}
          </div>
          <GoodsCommentImage :pictures="item.pictures" />
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan">
              <i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}
            </span>
          </div>
        </div>
      </div>
    </div>
    <!-- 分页按钮 -->
    <XtxPagination
      :pages="commentListData.pages"
      v-model:page="reqparams.page"
    />
  </div>
</template>

<script>
import { getCommentData, getCommentListApi } from "../../api/goods";
import { ref, watch } from "vue";
import { useRoute } from "vue-router";
import GoodsCommentImage from "./GoodsCommentImage";

export default {
  name: "GoodsComment",
  setup() {
    let { commentData } = fnGetComment();
    // 存放 title 值
    let titleLight = ref("全部评价");
    let {
      commentListData,
      formatAttrs,
      formatNickname,
      updateCommentList,
      reqparams,
    } = fnGetCommentList();

    // 点击高亮
    const handleButtonLight = (value) => {
      titleLight.value = value.title;
      updateCommentList(value);
    };

    return {
      commentData,
      handleButtonLight,
      titleLight,
      commentListData,
      formatAttrs,
      formatNickname,
      reqparams,
    };
  },
  components: {
    GoodsCommentImage,
  },
};

// 获取各种评论数量
function fnGetComment() {
  const route = useRoute();
  let commentData = ref([]);

  getCommentData(route.params.id).then((res) => {
    // console.log(res.result);
    commentData.value = res.result;
    commentData.value.tags.unshift(
      {
        title: "全部评价",
        tagCount: commentData.value.evaluateCount,
      },
      {
        title: "有图",
        tagCount: commentData.value.hasPictureCount,
      }
    );

    // console.log(commentData.value);
  });

  return {
    commentData,
  };
}

// 获取评论列表
function fnGetCommentList() {
  const route = useRoute();
  let commentListData = ref([]);
  // 存放请求参数
  let reqparams = ref({
    page: 1,
    pageSize: 10,
    hasPicture: false,
    tag: "",
    sortField: "",
    sortMethod: "",
  });

  // 获取评论列表
  const getCommentList = (id, params) => {
    getCommentListApi(id, params).then((res) => {
      // console.log(res.result);
      commentListData.value = res.result;
    });
  };

  // 更新评论列表
  const updateCommentList = (value) => {
    // console.log(value);
    if (value.title === "有图") {
      reqparams.value.page = 1;
      reqparams.value.hasPicture = true;
    } else if (value.title === "全部评价") {
      reqparams.value.page = 1;
      reqparams.value.tag = "";
    } else {
      reqparams.value.page = 1;
      reqparams.value.tag = value.title;
    }
  };

  getCommentList(route.params.id, reqparams.value);

  // 格式化商品属性信息
  const formatAttrs = (specs) => {
    return specs.map((spec) => `${spec.name}: ${spec.nameValue}`).join(" ");
  };

  // 格式化用户昵称
  const formatNickname = (nickname) => {
    return nickname.substr(0, 1) + "****" + nickname.substr(-1);
  };

  watch(reqparams.value, () => {
    getCommentList(route.params.id, reqparams.value);
  });

  return {
    commentListData,
    formatAttrs,
    formatNickname,
    updateCommentList,
    reqparams,
  };
}
</script>

<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;

    .data {
      width: 340px;
      display: flex;
      padding: 20px;

      p {
        flex: 1;
        text-align: center;

        span {
          display: block;

          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }

          &:last-child {
            color: #999;
          }
        }
      }
    }

    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;

      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }

      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;

        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;

          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }

          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }

  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;

    > span {
      margin-left: 20px;
    }

    > a {
      margin-left: 30px;

      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }

  .list {
    padding: 0 20px;

    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;

      .user {
        width: 160px;

        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }

        span {
          padding-left: 10px;
          color: #666;
        }
      }

      .body {
        flex: 1;

        .score {
          line-height: 40px;

          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }

          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }

      .text {
        color: #666;
        line-height: 24px;
      }

      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
